package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;

public class SmsBatchSchedTask implements CallBackInvoke {
    private static final Log log = LogFactory.getLog(SmsBatchSchedTask.class);
    private String charset = "utf-8"; //字符集
    private int timeout = 100000;//请求超时时间
    private int enableSSLflag = 0;

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String body) {
        int taskstate = map.get("state");  //任务状态
        int batchstate = map.get("batchstate"); //批次状态
        int tosendsum = map.get("tosendsum"); //已发送数量
        def batch_id = map.get("batch_id"); //批次号
        def template_id = map.get("sms_id"); //模板ID
        if (batchstate == -1) {
            log.debug("===========" + batch_id + "暂停批次，不处理后续进度================");
            return;
        } else if (batchstate == -2 && taskstate > 0) {
            log.debug("===========" + batch_id + "废弃批次处理，数据删除================");
            String[] data = new String[3];
            data[0] = "delete from sms_send_sync where batch_id='" + batch_id + "'";
            data[1] = "delete from sms_send_task where batch_id='" + batch_id + "'";
            data[2] = "delete from sms_send_batch where batch_id='" + batch_id + "'";
            int[] dataresult = eliteBean.updateBatch(data, "dataSource");
            return;
        };
        PropertiesUtil properties = new PropertiesUtil();
        def ccserverip = properties.readValue("ccserverip"); //获取CallCenter访问地址
        def interfacename, interfaceflag, smap;
        int findrow = 0;
        if (taskstate == -1) {
            log.debug("===========" + batch_id + "调用安正接口，等待数据返回================");
            interfacename = "SmsBatchRequest";
            interfaceflag = 1;
        } else if (taskstate == 0) {
            log.debug("===========" + batch_id + "等待安正数据返回================");
            return;
        } else if (taskstate == 1) {
            //20190702短信批次增加安正数据回写完成时间节点
            eliteBean.update("update sms_send_batch set completedate=sysdate where batch_id='" + batch_id + "'", "dataSource");

            log.debug("===========" + batch_id + "生成短信待发任务================");
            interfacename = "SmsCreate";
            interfaceflag = 1;
        } else if (taskstate == 3 && tosendsum > 0) {
            smap = eliteBean.chaxun_sql("select count(*) as findrow from sms_send_task where batch_id='" + batch_id + "'", "dataSource");
            findrow = smap[0].get("findrow");
            if (findrow > 0) {
                return
            };
            log.debug("===========" + batch_id + "批次数据发送完成================");
            eliteBean.update("update sms_send_batch set state=4 where batch_id='" + batch_id + "'", "dataSource");
            return;
        };
        if (interfaceflag == 1) {
            def url = "http://" + ccserverip + "/EliteGeneralHttpTask/GeneralService/" + interfacename;
            def jsonbody = "{\"batch_id\":\"" + batch_id + "\",\"template_id\":\"" + template_id + "\",\"requesttype\": 1}";
            Util.doHttpPost(url, jsonbody, charset, timeout, enableSSLflag);
        };
    }
}